<?php
/*
	
*/
defined('BY_JYA') or exit('Access Denied');
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
$basic=$this->plugin_setting('basic');
$return['banner']=$basic['image'];
$return['basic']=$basic;
set_html('title','发布任务');
if($_RQ['op']=='main'){
	//查询在进行的项目
	//是否关注
	$return['isfollow']=pdo_getcolumn('core_fans',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']),'follow');	
	if($id){
		$post=$this->pdo_get('renwu',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}else{
		$post=array('steps'=>array());
	}
	$member=$this->pdo_get('member',array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
	if($return['pluginset']['renwuup_shenhe'] && $member['status']!=1){
		exi('会员申请通过后，才能发布任务哦！','error','/myhome/');
	}
	$return['renwu_types']=$this->renwu_types;
	$return['hours_type']=$this->hours_type;
	$return['times_type']=$this->times_type;
	$return['steps_type']=$this->steps_type;
	$return['shenhedays']=$this->shenhedays;
	$return['params']=array();
	$return['post']=$post;
	$return['member']=$member;
	$return['os']=$_SESSION['os'];
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	if(!$postdata['title']){
			exi('任务标题不能为空！','error');
	}
	$num=mb_strlen($postdata['title']);
	if($num>30){
			exi('任务标题不能超过10个汉字！','error');
	}
	if(!$postdata['desc']){
			exi('任务描述不能为空！','error');
	}
	if(!$postdata['image']){
			//exi('任务图片不能为空！','error');
	}
	if(!$postdata['fee']){
			exi('任务价格必填！','error');
	}
	if(!$postdata['totalnum']){
			exi('任务数量必填！','error');
	}
	if(!$postdata['steps']){
			exi('任务步骤必填！','error');
	}
	if($basic['needsh']){
			$status=0;
	}else{
			$status=1;
	}
	$insert=array(
			'type'=>$postdata['type'],
			'title'=>$postdata['title'],
			'desc'=>$postdata['desc'],
			'image'=>$postdata['image'],
			'ad_image'=>$postdata['ad_image'],
			'ad_video'=>$postdata['ad_video'],
			'helpurl'=>$postdata['helpurl'],
			'tuiurl'=>$postdata['tuiurl'],
			'goodsid'=>$postdata['goodsid'],
			'steps'=>ijson_encode($postdata['steps']),
			'hours_type'=>$postdata['hours_type'],
			'times_type'=>$postdata['times_type'],
			'totalnum'=>$postdata['totalnum'],
			'shenhedays'=>$postdata['shenhedays'],
			'fee'=>$postdata['fee'],
			'status'=>$status,
	);
	if($postdata['id']){
			$this->pdo_update('renwu',$insert,array('uniacid'=>$_SESSION['uniacid'],'id'=>$postdata['id']));
			exi('更新任务成功','success','jy_renwushop/renwu.mylist/');
	}else{
		    $payfee=$insert['fee']*$insert['totalnum'];
			$this->model('member')->recharge($_SESSION['uid'],$payfee,2,'发任务消费保证金');
			$insert['mid']=$_SESSION['uid'];
			$insert['uniacid']=$_SESSION['uniacid'];
			$insert['createtime']=TIMESTAMP;
			$this->pdo_insert('renwu',$insert);
			$id=pdo_insertid();
			$params=array(
				'mid'=>$_SESSION['uid'],
				'msg'=>'发任务消费保证金',
				'fee'=>$payfee,
				'status'=>1,
				'renwuid'=>$id,
				'isadd'=>0,
			);
			$this->model('model')->set_feelog($params,3);
			exi('发布任务成功','success','jy_renwushop/renwu.mylist/');
	}
}